package model

import (
	"log"

	"gitee.com/hust-ideas/little-genius-backend/config"
	"gitee.com/hust-ideas/little-genius-backend/dal/db/keywords"
	"gitee.com/hust-ideas/little-genius-backend/dal/db/user"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type DBModel struct {
	KeywordsModel keywords.KeywordsModelInterface
	UserModel     user.UserModelInterface
}

func InitDB(config config.Config) *DBModel {
	db, err := gorm.Open(mysql.Open(config.GetMysqlDataSource()), &gorm.Config{})
	if err != nil {
		log.Fatalf("failed to connect to database: %v", err)
	}

	db.AutoMigrate(&keywords.Keywords{}, &user.User{})

	log.Printf("connected to database on %s", config.GetMysqlDataSource())

	var keywordsModel keywords.KeywordsModelInterface = keywords.NewKeywordsModel(db)
	var userModel user.UserModelInterface = user.NewUserModel(db)
	return &DBModel{
		KeywordsModel: keywordsModel,
		UserModel:     userModel,
	}
}
